<script setup lang="ts">
import { reqGetKnowledgePage } from '@/services/consult'
import type { KnowledgeList, KnowledgeParams, KnowledgeType } from '@/types/consult'
import KnowledgeCard from '@/views/Home/components/KnowledgeCard.vue'
import { ref } from 'vue'

// 加载状态
const loading = ref(false)
// 加载完成
const finished = ref(false)
// 接受父组件传参
const props = defineProps<{ type: KnowledgeType }>()
// 声明一个数组
const list = ref<KnowledgeList>([])
// 准备的参数
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})
// 触发加载函数
const onLoad = async () => {
  // 发请求拿数据
  const res = await reqGetKnowledgePage(params.value)
  // console.log(res)
  // 往数组中添加数据
  list.value.push(...res.data.rows)
  // 如果数组的总长度大于数据的total就把finished改为true
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    // 反之current++
    params.value.current++
  }
  // 关闭加载
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="(item, i) in list" :key="i" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
